package site

import org.springframework.dao.DataIntegrityViolationException
import grails.plugins.springsecurity.Secured



class EquipeController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def index() {
        redirect(action: "list", params: params)
    }
    
    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [equipeInstanceList: Equipe.list(params), equipeInstanceTotal: Equipe.count()]
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def create() {
        [equipeInstance: new Equipe(params)]
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def save() {
        def equipeInstance = new Equipe(params)
        if (!equipeInstance.save(flush: true)) {
            render(view: "create", model: [equipeInstance: equipeInstance])
            return
        }

        flash.message = message(code: 'default.created.message', args: [message(code: 'equipe.label', default: 'Equipe'), equipeInstance.id])
        redirect(action: "show", id: equipeInstance.id)
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def show() {
        def equipeInstance = Equipe.get(params.id)
        if (!equipeInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'equipe.label', default: 'Equipe'), params.id])
            redirect(action: "list")
            return
        }

        [equipeInstance: equipeInstance]
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def edit() {
        def equipeInstance = Equipe.get(params.id)
        if (!equipeInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'equipe.label', default: 'Equipe'), params.id])
            redirect(action: "list")
            return
        }

        [equipeInstance: equipeInstance]
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def update() {
        def equipeInstance = Equipe.get(params.id)
        if (!equipeInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'equipe.label', default: 'Equipe'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (equipeInstance.version > version) {
                equipeInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                    [message(code: 'equipe.label', default: 'Equipe')] as Object[],
                          "Another user has updated this Equipe while you were editing")
                render(view: "edit", model: [equipeInstance: equipeInstance])
                return
            }
        }

        equipeInstance.properties = params

        
        try{
            if (!equipeInstance.save(flush: true)) {
                render(view: "edit", model: [equipeInstance: equipeInstance])
                return
            }
           
        }catch(Exception e){
            render(view: "edit", model: [equipeInstance: equipeInstance])
            flash.message = "Erro ao Editar"
        }
        

        flash.message = message(code: 'default.updated.message', args: [message(code: 'equipe.label', default: 'Equipe'), equipeInstance.id])
        redirect(action: "show", id: equipeInstance.id)
    }

    @Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
    def delete() {
        def equipeInstance = Equipe.get(params.id)
        if (!equipeInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'equipe.label', default: 'Equipe'), params.id])
            redirect(action: "list")
            return
        }

        try {
            equipeInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'equipe.label', default: 'Equipe'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'equipe.label', default: 'Equipe'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
    
    def view = {
        def banner = Equipe.get( params.id )
        if (banner) {
            response.setContentLength(banner.imagem.size())
            response.setHeader('filename', banner.titulo)
            OutputStream out = response.outputStream
            out.write(banner.imagem)
            out.close()
        }
        else {
            response.sendError(404)
        }
    }
}
